package com.mi.service.impl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mi.entity.MyPages;
import com.mi.entity.NewsEntity;
import com.mi.entity.ProductEntity;
import com.mi.mapper.NewsMapper;
import com.mi.service.NewsService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.swing.text.html.parser.Entity;
import java.util.List;

@Service
@com.alibaba.dubbo.config.annotation.Service
public class NewsServiceImpl implements NewsService {

    @Autowired(required = false)
    NewsMapper newsMapper;

    @Override
    public MyPages<NewsEntity> queryNews(NewsEntity ne) {

        PageHelper.startPage(ne.getPageNum(),ne.getPageSize());
        List<NewsEntity> list = newsMapper.queryNewsMapper(ne);
        PageInfo<NewsEntity> pageInfo = new PageInfo<>(list);
        MyPages<NewsEntity> newsPages = new MyPages<>();
        newsPages.setList(pageInfo.getList());
        newsPages.setPageNum(pageInfo.getPageNum());
        newsPages.setPageSize(pageInfo.getPageSize());
        newsPages.setPages(pageInfo.getPages());
        newsPages.setPageCount((int)pageInfo.getTotal());

        return newsPages;

    }

    //根据条件查询
    @Override
    public MyPages<NewsEntity> queryNewss(NewsEntity newsEntity){
        PageHelper.startPage(newsEntity.getPageNum(),newsEntity.getPageSize());
        List<NewsEntity> list = newsMapper.queryNews(newsEntity);
        PageInfo<NewsEntity> pageInfo = new PageInfo<>(list);
        MyPages<NewsEntity> newsPages = new MyPages<>();
        newsPages.setList(pageInfo.getList());
        newsPages.setPageNum(pageInfo.getPageNum());
        newsPages.setPageSize(pageInfo.getPageSize());
        newsPages.setPages(pageInfo.getPages());
        newsPages.setPageCount((int)pageInfo.getTotal());
        return newsPages;
    }

    //根据新闻名模糊查询
    @Override
    public MyPages<NewsEntity> fuzzyQuery(NewsEntity ne) {

        ne.setPageSize(1);

        PageHelper.startPage(ne.getPageNum(),ne.getPageSize());

        List<NewsEntity> list = newsMapper.fuzzyQuery(ne);

        PageInfo<NewsEntity> pageInfo = new PageInfo<>(list);
        MyPages<NewsEntity> newsPages = new MyPages<>();
        newsPages.setList(pageInfo.getList());
        newsPages.setPageNum(pageInfo.getPageNum());
        newsPages.setPageSize(pageInfo.getPageSize());
        newsPages.setPages(pageInfo.getPages());
        newsPages.setPageCount((int)pageInfo.getTotal());

        return newsPages;
    }

    @Override
    public NewsEntity queryDetails(int n_id) {
        return newsMapper.queryNewsDetails(n_id);
    }

    /**
     * 添加新闻
     * @param newsEntity
     * @return
     */
    public boolean addNews(NewsEntity newsEntity){
        return newsMapper.addNews(newsEntity);
    }

    /**
     * 删除新闻
     * @param n_id
     * @return
     */
    public boolean delNews( String[] n_id){
        return newsMapper.delNews(n_id);
    }

    /**
     * 修改新闻状态
     * @param newsEntity
     * @return
     */
    public boolean updNewsStatus(NewsEntity newsEntity){
        return newsMapper.updNewsStatus(newsEntity);
    }

}
